package base;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import utils.GlideUtils;


public class BaseFragment extends Fragment {


    // 第一次按下返回键的时间
    private static long m_FirstTime;
    /** Fragment当前状态是否可见 */

    protected boolean isVisible;

    /**
     * 判断本地网络是否连接
     *
     * @param paramContext
     * @return
     */
    public static boolean isConnect(Context paramContext) {
        ConnectivityManager localConnectivityManager = (ConnectivityManager) TheApplication.instance
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (localConnectivityManager != null) {
            NetworkInfo localNetworkInfo = localConnectivityManager
                    .getActiveNetworkInfo();
            if ((localNetworkInfo != null)
                    && (localNetworkInfo.isConnected())
                    && (localNetworkInfo.getState() == NetworkInfo.State.CONNECTED))
                return true;
        }
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /**
     * 显示图片
     *
     * @param view
     * @param strHttpUrl
     */
    public void display(ImageView view, String strHttpUrl) {
        GlideUtils.loadImageView(getActivity().getApplication(),strHttpUrl,view);
    }

    /**
     * @param view
     * @param strHttpUrl
     * @param loadingResId 加载中与加载失败为同一张图片
     */
    public void display(ImageView view, String strHttpUrl, int loadingResId) {
        GlideUtils.loadImageViewLoding(getActivity().getApplication(),strHttpUrl,view,loadingResId,loadingResId);
    }

    protected Activity mactivity;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mactivity=activity;
    }





   @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

// TODO Auto-generated method stub

        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint()) {

            isVisible = true;

            onVisible();

        } else {

            isVisible = false;

            onInvisible();

        }

    }





    /** 可见 */

    protected void onVisible() {
        Log.e("mactivity","mactivity");
        if (mactivity!=null){

            lazyLoad();
        }
    }



    /**不可见*/

    protected void onInvisible() {



    }



    /**延迟加载    子类必须重写此方法 */

    protected void  lazyLoad(){

    };

}
